<template>
  <div class="mark-down-container">
    <div class="mark-down-input">
      <div class="mark-down-title">
        <input v-model="dataInfo.title" placeholder="请输入标题" />
        <div class="btn">
          <a-button
            type="primary"
            :loading="nextLoading"
            size="small"
            @click="dataInfo.id ? modify() : preservation()"
            >保存</a-button
          >
        </div>
      </div>
      <textarea :value="dataInfo.data.value" placeholder="# hello word" @input="update"></textarea>
    </div>
    <div class="mark-down-box">
      <div class="mark-down-title">{{ dataInfo.title }}</div>
      <div v-html="compiledMarkdown"></div>
    </div>
  </div>
</template>
<script>
  import markdownIt from '@/plugins/markdown/markdown-it.js';

  import io from '@/utils/http/index.js';
  import { message } from 'ant-design-vue';

  export default {
    components: {},
    props: {
      showData: {
        type: Object,
      },
    },
    data() {
      return {
        dataInfo: {},
        nextLoading: false,
      };
    },
    computed: {
      compiledMarkdown() {
        return new markdownIt('commonmark').render(this.dataInfo.data.value);
      },
    },
    created() {
      if (this.showData) {
        this.dataInfo = { ...this.showData };
      }
    },
    methods: {
      update(e) {
        this.dataInfo.data.value = e.target.value;
      },

      getRequest() {
        return {
          type: this.dataInfo.type,
          title: this.dataInfo.title,
          data: JSON.stringify(this.dataInfo.data),
        };
      },

      // 保存
      preservation() {
        this.nextLoading = true;
        io({
          url: '/show/api/module',
          method: 'post',
          data: this.getRequest(),
        }).then((res) => {
          message.success('保存成功');
          this.nextLoading = false;
          this.$router.replace({ path: '/make/' + res.data._id, query: this.$route.query });
        });
      },

      // 修改
      modify() {
        this.nextLoading = true;
        io({
          url: '/show/api/module/' + this.id,
          method: 'put',
          data: this.getRequest(),
        }).then((res) => {
          message.success('修改成功');
          this.nextLoading = false;
        });
      },
    },
  };
</script>
<style lang="scss">
  .mark-down-container {
    height: 100%;
    .mark-down-input {
      height: 100%;
      border-right: 1px solid #ccc;
      position: relative;
      display: inline-block;
      width: 49%;
      background-color: #fff;
      textarea {
        border: none;
        resize: none;
        outline: none;
        font-size: 14px;
        font-family: 'Monaco', courier, monospace;
        padding: 50px 18px 10px;
        width: 100%;
        height: 100%;
        line-height: 28px;
        vertical-align: top;
      }
    }

    .mark-down-box {
      display: inline-block;
      width: 49%;
      height: 100%;
      vertical-align: top;
      padding: 50px 15px 10px;
      position: relative;
    }

    .mark-down-title {
      position: absolute;
      left: 0;
      top: 0;
      height: 50px;
      padding-left: 15px;
      font-size: 18px;
      line-height: 40px;
      font-weight: 500;
      padding-top: 10px;
      width: 100%;
      input {
        height: 40px;
        border: 0;
        outline: none;
        font-size: 18px;
        font-weight: 500;
        width: 100%;
      }
      .btn {
        position: absolute;
        right: 15px;
        top: 10px;
      }
    }
  }
</style>
